﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimulateStock
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
        /// <summary>
        ///得到数组的最小值
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        private int GetMinOfArray(int[] array)
        {
            int result = int.MaxValue;

            for (int index = 0; index < array.Length; index++)
            {
                if (result >= array[index])
                {
                    result = array[index];
                }
            }

            return result;
        }
        /// <summary>
        /// 得到数组的最大值
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        private int GetMaxOfArray(int[] array)
        {
            int result = int.MinValue;

            for (int index = 0; index < array.Length; index++)
            {
                if (result <= array[index])
                {
                    result = array[index];
                }
            }

            return result;
        }
        
        private void btnGenerate_Click(object sender, EventArgs e)
        {

            int length = int.Parse(this.txtLength.Text);
            int from = int.Parse(this.txtFrom.Text);
            int to = int.Parse(this.txtTo.Text);

            int[] numbers = new int[length];
            //对数组中的每一个值给定一个大小从from到to的随机值
            Random random = new Random();
            for (int index = 0; index < numbers.Length; index++)
            {
                numbers[index] = random.Next(from, to);
            }
            //获取数组的最大值和最小值
            int max = this.GetMaxOfArray(numbers);
            int min = this.GetMinOfArray(numbers);
            this.lblMaxMin.Text = string.Format("最大：{0}   最小:{1}", max, min);

            //设置x方向和y方向的单位
            float xPer = (float)this.panShow.Width / numbers.Length;
            float yPer = (float)this.panShow.Height / to;

            PointF[] points = new PointF[numbers.Length];

            Graphics grp = this.panShow.CreateGraphics();
            grp.Clear(this.panShow.BackColor);

            //根据数组中的每个随机数依次绘制图形，如果该值为数组最大值，最小值则分别用正方形和圆形特别标出，并且依次用直线连接各点
            for (int index = 0; index < numbers.Length; index++)
            {
                PointF point = new PointF();
                point.X = index * xPer;
                point.Y = this.panShow.Height - numbers[index] * yPer;

                //最大值，用矩形标出
                if (numbers[index] == max)
                {
                    RectangleF rect = new Rectangle();
                    rect.X = point.X - 8f;
                    rect.Y = point.Y - 8f;
                    rect.Width = rect.Height = 2 * 8f;
                    grp.DrawRectangle( Pens.Black , rect.X, rect.Y, rect.Width, rect.Height);
                }

                //最小值，用圆形标注
                if (numbers[index] == min)
                {
                    RectangleF rect = new Rectangle();
                    rect.X = point.X - 8f;
                    rect.Y = point.Y - 8f;
                    rect.Width = rect.Height = 2 * 8f;
                    grp.DrawEllipse(Pens.Black, rect);
                }

                grp.DrawString(numbers[index].ToString(),
                    new Font("黑体", 12f), Brushes.Blue, point);

                points[index] = point;
            }
            grp.DrawLines(Pens.Red, points);
        }
    }
}
